package com.bdzl.auth.controller;


import com.alibaba.fastjson.JSONObject;
import com.bdzl.auth.common.utils.MD5Utils;
import com.bdzl.auth.service.*;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.util.xss.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;




/**
 * @Author Zhn
 * @Description //TODO 微信专用登录
 * @Date 11:40 2021/5/11
 * @Param
 * @return
 **/
@Controller
@RequestMapping("/weChat")
public class WeChatLoginController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;


    /**
     * 获取当前微信绑定账号列表
     * @param code 微信小程序传过来的参数：同微信小程序wx.login返回的code
     * @return
     */
    @GetMapping("/checkLogin")
    @ResponseBody
    public R checkLogin(String code){
        // 获取微信小程序的openID
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        //替换路径
        url = url
                .replace("APPID", "wx63cd236b67783e26")
                .replace("SECRET","598dd478816aca8af38c79799aad4d79")
                .replace("JSCODE", code);
        JSONObject jsonData = HttpRequest.getJsonData(url, null);
        //获取openid
        String openId = jsonData.getString("openid");
        if (openId==null){
            R r = R.error("获取openid失败");
            r.put("information",jsonData);
            return r;
        }
        //根据openid获取账号列表
        List<UserDO> byUserOpenid = userService.getByUserOpenid(openId);
        R r = R.ok(byUserOpenid);
        r.put("openId",openId);
        return r;
    }

  /**
   * @Author Zhn
   * @Description //TODO 快捷登录 根据ID获取用户调用登录接口
   * @Date 14:21 2021/5/11
   * @Param [userId] 用户ID
   * @return com.bdzl.pub.util.param.R
   **/
    @GetMapping("/quickLogin")
    @ResponseBody
    public R quickLogin(Integer userId){
        //根据用户ID获取用户
        UserDO userDO = userService.get(userId);
        //调用登录接口
        R login = this.login(userDO);
        return login;
    }

    /**
     * 登录接口
     * @param user 用户
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody UserDO user){
        //调用登录验证
        R login = loginService.login(user);
        //调用登录
        R r = loginService.userInfo(user, login);
        //查询账户是否绑定，未绑定进行绑定、
        UserDO sysUser = userService.getByUserLgname(user.getUserLgname());
        if (sysUser.getUserOpenid()==null||sysUser.getUserOpenid().equals("")){
            sysUser.setUserOpenid(user.getUserOpenid());
            userService.update(sysUser);
        }
        return r;
    }

    /**
     * 获取当前微信绑定账号列表
     * @param openId 微信传过来的参数
     * @return
     */
    @GetMapping("/bundlingList")
    @ResponseBody
    public R bundlingList(String openId){
        //根据openid获取账号列表
        List<UserDO> byUserOpenid = userService.getByUserOpenid(openId);
        R r = R.ok(byUserOpenid);
        r.put("openId",openId);
        return r;
    }
}
